package jobportal.student;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

public class HallTicket extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session=request.getSession();
        int id=Integer.parseInt(request.getParameter("jobid"));
        ServletOutputStream servletOutputStream = response.getOutputStream();
        HashMap parameterMap = new HashMap();
        parameterMap.put("email", (String)session.getAttribute("username"));
        parameterMap.put("jobid", id);
        parameterMap.put("cdaclogo",request.getRealPath("/images/cdac.gif"));
        parameterMap.put("erdcilogo",request.getRealPath("/images/erdci.jpg"));
        Connection con=Dbconn.getConn();
        InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/report/HallTicket.jasper");
        try {
            JasperRunManager.runReportToPdfStream(reportStream,
                    servletOutputStream, parameterMap, con);
            response.setContentType("application/pdf");
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch (Exception e) {
// display stack trace in the browser
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
        }
    }
}
